﻿using System;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class USLicenseAgreementWindow : EditorWindow
{
    private static string editorPrefsAgreement = ("uSequencer - Lisence Agreement V" + USUpgradePaths.CurrentVersionNumber);
    private static string fileContents = string.Empty;
    private bool hasAgreed;
    private static Vector2 scrollPosition = Vector2.zero;

    private void CloseAgreement()
    {
        base.Close();
    }

    private void OnGUI()
    {
        if (fileContents.Length == 0)
        {
            byte[] byteResource = USSequencerUtility.GetByteResource("uSequencer - License.txt");
            if (byteResource.Length != 0)
            {
                fileContents = Encoding.ASCII.GetString(byteResource);
            }
            else
            {
                StreamReader reader = new StreamReader(Application.dataPath + "/uSequencerBundledAssets/uSequencer - License.txt");
                fileContents = reader.ReadToEnd();
                reader.Close();
            }
        }
        if (!EditorPrefs.HasKey(editorPrefsAgreement))
        {
            EditorPrefs.SetBool(editorPrefsAgreement, false);
        }
        GUILayout.BeginVertical(new GUILayoutOption[0]);
        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, new GUILayoutOption[0]);
        GUI.enabled = false;
        EditorGUILayout.TextArea(fileContents, new GUILayoutOption[0]);
        GUI.enabled = true;
        EditorGUILayout.EndScrollView();
        GUILayout.BeginHorizontal(new GUILayoutOption[0]);
        this.hasAgreed = GUILayout.Toggle(this.hasAgreed, "I agree to the terms of this license.", new GUILayoutOption[0]);
        if (this.hasAgreed)
        {
            GUILayoutOption[] optionArray1 = new GUILayoutOption[] { GUILayout.MaxWidth(120f) };
            if (GUILayout.Button("Open uSequencer", optionArray1))
            {
                EditorPrefs.SetBool(editorPrefsAgreement, true);
                OpenUSequencer();
                this.CloseAgreement();
            }
        }
        GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.MaxWidth(120f) };
        if (GUILayout.Button("Close uSequencer", options))
        {
            this.CloseAgreement();
        }
        GUILayout.EndHorizontal();
        GUILayout.EndVertical();
    }

    private static void OpenUSequencer()
    {
        USWindow window = EditorWindow.GetWindow(typeof(USWindow), false, "uSequencer") as USWindow;
        window.autoRepaintOnSceneChange = true;
        window.position = new Rect(250f, 100f, 600f, 200f);
        window.minSize = window.minWindowSize;
        USWelcomeScreenWindow.OpenWindow(false);
    }

    public static void OpenWindow()
    {
        if (EditorPrefs.HasKey(editorPrefsAgreement))
        {
            EditorPrefs.DeleteKey(editorPrefsAgreement);
        }
        OpenUSequencer();
    }
}

